#pragma once
#include "MemoryDefined.h"

#define _FLV_CONVERT_

class DLL_PUBLIC_GMINSTANCE CFrameBuffer
{
public:
	CFrameBuffer(void);
	~CFrameBuffer(void);
public:
	BOOL	m_bln_inited;
	BOOL	m_bln_broadcastable;
	BOOL	m_bln_using_available;
	int		m_int_reference_count;

	int		m_int_stream_id;
	STREAM_CONFIGURE_INFO m_stu_info;

	void*	m_ptr_stream_source;

	void	init_variable();

	//////////////////////////
	// Original data info
	//////////////////////////
private:
	CRITICAL_SECTION	m_cs_video_lock;
	CRITICAL_SECTION	m_cs_audio_lock;
	//BOOL	m_bln_video_lock;
	//BOOL	m_bln_audio_lock;

public:
	int		m_int_video_codec;
	int		m_int_audio_codec;
	__int64	m_i64_video_timestamp;
	__int64	m_i64_audio_timestamp;

	// For video fifo
public:
	int		m_int_video_frames;
	char	m_buf_video_total[LIMIT_VIDEO_FRAME_TOTAL_SIZE];
	char	m_buf_video_temp[LIMIT_VIDEO_FRAME_SIZES];
	int		m_buf_video_position[LIMIT_FRAME_FIFO_COUNT];
	char	m_buf_video_status[LIMIT_FRAME_FIFO_COUNT];
	int		m_int_video_pushindex;
	int		m_int_video_popindex;

	// For audio fifo
public:
	int		m_int_audio_frames;
	char	m_buf_audio_total[LIMIT_AUDIO_FRAME_TOTAL_SIZE];
	char	m_buf_audio_temp[LIMIT_AUDIO_FRAME_SIZES];
	int		m_buf_audio_position[LIMIT_FRAME_FIFO_COUNT];
	char	m_buf_audio_status[LIMIT_FRAME_FIFO_COUNT];
	int		m_int_audio_pushindex;
	int		m_int_audio_popindex;

	int		m_int_pop_bytes_per_one;
	int		m_int_pop_duration;
	int		m_int_pop_video_delay_time;		// video pop delay time(ms)

public:
	void	video_lock();
	void	video_unlock();
	void	audio_lock();
	void	audio_unlock();

	void	video_push_back(int frame_size, char* frame_buffer, BOOL keyframe);
	void	video_pop_front(int& pop_index, int& frame_size, char* frame_buffer, BOOL& keyframe);
	void	video_clear();
	int		video_size();

	void	audio_push_back(int frame_size, char* frame_buffer);
	void	audio_pop_front(int& audio_timestamp, int& frame_size, char* frame_buffer);
	void	audio_clear();
	int		audio_size();

	//////////////////////////
	// Converted data info
	//////////////////////////
private:
	CRITICAL_SECTION	m_cs_converted_video_lock;
	CRITICAL_SECTION	m_cs_converted_audio_lock;
	//BOOL	m_bln_converted_video_lock;
	//BOOL	m_bln_converted_audio_lock;

public:
	int		m_int_converted_video_codec;
	int		m_int_converted_audio_codec;
	__int64	m_i64_converted_video_timestamp;
	__int64	m_i64_converted_audio_timestamp;

	// For video fifo
public:
	int		m_int_converted_video_frames;
	char	m_buf_converted_video_total[LIMIT_VIDEO_FRAME_TOTAL_SIZE];
	char	m_buf_converted_video_temp[LIMIT_VIDEO_FRAME_SIZES];
	int		m_buf_converted_video_position[LIMIT_FRAME_FIFO_COUNT];
	char	m_buf_converted_video_status[LIMIT_FRAME_FIFO_COUNT];
	int		m_int_converted_video_pushindex;
	int		m_int_converted_video_popindex;

	// For audio fifo
public:
	int		m_int_converted_audio_frames;
	char	m_buf_converted_audio_total[LIMIT_AUDIO_FRAME_TOTAL_SIZE];
	char	m_buf_converted_audio_temp[LIMIT_AUDIO_FRAME_SIZES];
	int		m_buf_converted_audio_position[LIMIT_FRAME_FIFO_COUNT];
	char	m_buf_converted_audio_status[LIMIT_FRAME_FIFO_COUNT];
	int		m_int_converted_audio_pushindex;
	int		m_int_converted_audio_popindex;

public:
	void	converted_video_lock();
	void	converted_video_unlock();
	void	converted_audio_lock();
	void	converted_audio_unlock();

	void	converted_video_push_back(int frame_size, char* frame_buffer, BOOL keyframe);
	void	converted_video_pop_front(int& pop_index, int& frame_size, char* frame_buffer, BOOL& keyframe);
	void	converted_video_clear();
	int		converted_video_size();

	void	converted_audio_push_back(int frame_size, char* frame_buffer);
	void	converted_audio_pop_front(int& audio_timestamp, int& frame_size, char* frame_buffer);
	void	converted_audio_clear();
	int		converted_audio_size();

	//////////////////////////
	// Raw data info
	//////////////////////////
private:
	CRITICAL_SECTION	m_cs_raw_video_lock;
	//BOOL	m_bln_raw_video_lock;

public:
	int		m_int_raw_video_codec;
	__int64	m_i64_raw_video_timestamp;

	// For video fifo
public:
	int		m_int_raw_video_frames;
	char	m_buf_raw_video_total[LIMIT_VIDEO_RAW_FRAME_TOTAL_SIZE];
	char	m_buf_raw_video_temp[LIMIT_VIDEO_RAW_FRAME_SIZES];
	int		m_buf_raw_video_position[LIMIT_FRAME_FIFO_COUNT];
	char	m_buf_raw_video_status[LIMIT_FRAME_FIFO_COUNT];
	int		m_int_raw_video_pushindex;
	int		m_int_raw_video_popindex;

public:
	void	raw_video_lock();
	void	raw_video_unlock();

	void	raw_video_push_back(int frame_size, char* frame_buffer, BOOL keyframe);
	void	raw_video_pop_front(int& pop_index, int& frame_size, char* frame_buffer, BOOL& keyframe);
	void	raw_video_clear();
	int		raw_video_size();

#if defined(_FLV_CONVERT_)
private:
	CRITICAL_SECTION	m_cs_flv_lock;
	//BOOL	m_bln_flv_lock;

public:
	int		m_int_flv_codec;
	__int64	m_i64_flv_timestamp;

	// For flv fifo
public:
	int		m_int_flv_frames;
	char	m_buf_flv_total[LIMIT_VIDEO_FRAME_TOTAL_SIZE];
	char	m_buf_flv_temp[LIMIT_VIDEO_FRAME_SIZES];
	int		m_buf_flv_position[LIMIT_FRAME_FIFO_COUNT];
	char	m_buf_flv_status[LIMIT_FRAME_FIFO_COUNT];
	int		m_int_flv_pushindex;
	int		m_int_flv_popindex;

public:
	void	flv_lock();
	void	flv_unlock();
	void	flv_push_back(int frame_size, char* frame_buffer, BOOL keyframe);
	void	flv_pop_front(int& pop_index, int& frame_size, char* frame_buffer, BOOL& keyframe);
	void	flv_clear();
	int		flv_size();

public:
	void	set_video_pop_info(int int_pop_video_delay_time);
	void	set_audio_pop_info(int int_pop_bytes_per_one, int int_pop_duration);
#endif
};

